import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../../../../../global/user/user_info.dart';
import '../../../../../../../../utils/component/dialog/bottom.dart';
import '../../../../../../../../utils/date_format/date_util.dart';
import '../../../../../../../../utils/httputils/api.dart';
import '../../../../../../../../utils/httputils/resultdata.dart';
import '../../../../../../../../utils/util.dart';
import 'index.dart';

class InventoryCreateController extends GetxController {
  InventoryCreateController();

  final state = InventoryCreateState();

  void actionSure() async {
    if (state.nameCtrl.text.isEmpty) {
      EasyLoading.showToast('请输入具体问题名称');
      return;
    }
    if (state.lead == null) {
      EasyLoading.showToast('请选择牵头人');
      return;
    }
    if (state.helps == null) {
      EasyLoading.showToast('请选择协同人');
      return;
    }
    if (state.resulveCtrl.text.isEmpty) {
      EasyLoading.showToast('请输入解决措施');
      return;
    }
    if (state.time == null) {
      EasyLoading.showToast('请选择完成时限');
      return;
    }

    ResultData res = await ProListApi.createTask(
        name: state.nameCtrl.text,
        reslve: state.resulveCtrl.text,
        time: state.time!,
        status: state.status.toString(),
        lead: state.lead!,
        helps: state.helps!);
    if (res.isSuccess) {
      Get.back(result: true);
    }
  }

  Future<void> getUserList() async {
    ResultData res = await MeApi.getUserList();
    if (res.isSuccess) {
      List data = res.data;
      state.users = data.map((e) => UserInfo.fromJson(e)).toList();
    }
  }

  void actionLead() async {
    if (state.users == null) {
      await getUserList();
    }
    showActionSheet(
      titles: state.users?.map((e) => e.userName ?? '').toList() ?? [],
      choosedIndexs: [state.leadIndex],
      onTap: (p0) {
        state.leadIndex = p0.first;
        state.lead = state.users![p0.first];
        update();
      },
    );
  }

  void actionHelps() async {
    if (state.users == null) {
      await getUserList();
    }
    showActionSheet(
      titles: state.users?.map((e) => e.userName ?? '').toList() ?? [],
      isMutiableChoose: true,
      choosedIndexs: state.helpIndexs,
      onTap: (p0) {
        state.helpIndexs = p0;
        state.lead = state.users![p0.first];
        state.helps = [];
        for (var i = 0; i < state.users!.length; i++) {
          if (p0.contains(i)) {
            var item = state.users![i];
            state.helps!.add(item);
          }
        }
        update();
      },
    );
  }

  void actionTime() {
    showModalBottomSheet(
        // 底部颜色为透明，用Container修饰顶部圆角
        backgroundColor: Colors.transparent,
        isScrollControlled: true,
        context: Get.context!,
        builder: (BuildContext content) {
          return SingleChildScrollView(
            child: Container(
              padding: EdgeInsets.only(top: 20.sp),
              clipBehavior: Clip.antiAlias,
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(20.0),
                  topRight: Radius.circular(20.0),
                ),
              ),
              child: Column(
                children: [
                  BrnCalendarView(
                    showControllerBar: true,
                    displayMode: DisplayMode.month,
                    selectMode: SelectMode.single,
                    dateChange: (date) {
                      state.date = date;
                      state.time = DateFormat.format(date, DateFormatType.ymd);
                      update();
                    },
                  ),
                  SafeArea(
                    child: Container(
                      margin: EdgeInsets.only(top: 10.sp, bottom: 5.sp),
                      width: Get.width / 5 * 4,
                      child: ElevatedButton(
                        onPressed: () {
                          Get.back();
                        },
                        style: ElevatedButton.styleFrom(
                          primary: Color(0xff58C165),
                          shape: StadiumBorder(),
                        ),
                        child: Text("确定"),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        });
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
